package condominio.servlet.residente;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import condominio.excepcion.DAOExcepcion;
import condominio.modelo.Persona;
import condominio.negocio.ResidenteNegocio;
/**
 * Servlet implementation class InsertarResidenteServlet
 */
@WebServlet("/InsertarResidenteServlet")
public class InsertarResidenteServlet extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet{
	static final long serialVersionUID = 1L;

    public InsertarResidenteServlet() {
        // TODO Auto-generated constructor stub
		super();
  }

	protected void doGet(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.processRequest(request, response);
	}
	
	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		Persona oUsuario = (Persona)session.getAttribute("USUARIO_ACTUAL");	
		if(oUsuario == null){
			request.getRequestDispatcher("error.jsp").forward(request, response);
			return;
		}		
		
		String nombre 		= request.getParameter("nombre");
		String apaterno 	= request.getParameter("apaterno");
		String amaterno 	= request.getParameter("amaterno");
		String dni		 	= request.getParameter("dni");
		String fecha = request.getParameter("anioRes") + "-"
		+ request.getParameter("mesRes") + "-"
		+ request.getParameter("diaRes");
		String telefono		= request.getParameter("telefono");
		String ecivil		= request.getParameter("ecivil");
		String correo		= request.getParameter("correo");
		String clave		= request.getParameter("clave");
		String tiporesidente= request.getParameter("tiporesidente");
		int coVivienda		= Integer.parseInt(request.getParameter("cmbVivienda"));
		int coEstacionamiento = Integer.parseInt(request.getParameter("cmbEstacionamiento"));

		ResidenteNegocio dao = new ResidenteNegocio();
		try {
			dao.insertResidente(nombre, apaterno, amaterno, dni, fecha,
			telefono, ecivil, correo, clave, tiporesidente, coVivienda, coEstacionamiento );
			response.sendRedirect(request.getContextPath() + "/menu.jsp");
		} catch (DAOExcepcion e) {
			session.setAttribute("ERROR_MSG", e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("/ResidenteServlet");
			rd.forward(request, response);
		}
	
	}	
	
}
